<?php
namespace App\Model\Backstage;
use App\Model\Base;
use Illuminate\Support\Facades\DB;

/**
 * Class RoomWordbookInfo
 * @package App\Model\Backstage
 * 楼盘字典 栋座详细信息model
 */
class RoomWordbookInfo extends Base
{
    //楼盘字典 - 栋座数据表
    protected  $table;
    public function __construct()
    {
        parent::__construct();
        $this->table = 'room_wordbook_info_'.$this->branch_id;  //后期分表
//        $this->table = 'room_wordbook_info';
    }
    //栋座名称单位
    public static $building_unit = ['栋',  '座', '幢', '号', '号楼'];

    //单元数单位
    public static $cell_unit = ['数字', '英文'];
    /**
     * @param $param
     * @return mixed
     * 添加楼盘字典 - 栋座详细信息
     */
    public function addWordbookInfo($param){
        $param['b_created_at'] = time();
        $param['b_updated_at'] = time();
        $param['b_building'] = $param['b_building'].self::$building_unit[$param['b_building_unit']];
        $param['b_uid'] = $this->uid;
        return DB::table($this->table)->insert($param);
    }


    /**
     * @param $param
     * 批量添加楼盘字典栋座信息
     */
    public function batchAddInfo($param){
        if($param['b_cell_unit'] == 1){
            if($param['b_cell_num'] > 26){
                $param['b_cell_num'] = 26;  //如果单元单位为英文 英文最多26个字母，所以数字不能大于26
            }
        }
        $temp_arr = $this->sequenceArray($param['b_type'], $param['b_start'], $param['b_end']);
        if($temp_arr['code'] != 1){
            return $temp_arr['code'];
        }
        unset($param['b_type']);
        unset($param['b_start']);
        unset($param['b_end']);
        $building_prefix = $param['b_building_prefix'];
        unset($param['b_building_prefix']);
        $insertArr = [];
        foreach($temp_arr['data'] as $k => $v){
            $param['b_created_at'] = time();
            $param['b_uid'] = $this->uid;
            $param['b_building'] = $building_prefix.$v.self::$building_unit[$param['b_building_unit']];
            array_push($insertArr, $param);
        }
        return DB::table($this->table)->insert($insertArr);
    }

    /**
     * @param $param
     * @return mixed
     * 修改楼盘字典 - 栋座信息
     */
    public function saveBuildingInfo($param){
        $param['b_updated_at'] = time();
        return DB::table($this->table)->where(['b_id' => $param['b_id']])->update($param);
    }

    /**
     * @param $id
     * @return mixed
     * 删除楼盘字典 - 栋座信息 此处做逻辑删除 后期考虑看房源表存什么字段，如果存名字可以硬删除，如果存ID需要逻辑删除
     */
    public function delBuildingInfo($id){
        $idArr = explode(',', $id);
        return DB::table($this->table)->whereIn('b_id',$idArr)->delete();
    }

    /**
     * @param int $pageLimit
     * @param $map
     * @return mixed
     * 查询楼盘字典对应的栋座信息
     */
    public function selectBuilding($map){
        $list = DB::table($this->table)
            ->where($map)
            ->select('b_id', 'b_building', 'b_building_unit', 'b_layer_num', 'b_ladder_num', 'b_door_num',
                'b_cell_num', 'b_cell_unit', DB::raw('from_unixtime(b_created_at) AS b_created_at'),
                DB::raw('from_unixtime(b_updated_at) AS b_updated_at'))
            ->orderBy('b_id', 'desc')
            ->paginate(env('PAGE_LIMIT_ADMIN'));
        if (sizeof($list) ==0) {
            return false;
        }
        return $list;
    }

    /**添加房源的时候根据楼盘字典的ID 获取该楼盘字典下面所有的栋座信息
     * @param $map
     * @return bool
     */
    public function selectBuildingforAdd($wId){
        $data = DB::table($this->table)
            ->where('b_w_id', $wId)
            ->select('b_id as label', 'b_building as value')
            ->get();
        if (sizeof($data) == 0) {
            return [];
        }
//        foreach ($data as $k=>$v){
//            $v->children = DB::table($this->table)->where('b_id', $v->label)
//                ->select('b_cell_num', 'b_cell_unit')
//                ->first();
//            if (self::$cell_unit[$v->children->b_cell_unit] == '英文') {
//                $english_arr = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z');
//                $num = [];
//                for ($i = 0; $i< $v->children->b_cell_num; $i++) {
//                    array_push($num, $english_arr[$i]);
//                }
//                $list = [];
//                foreach ($num as $key => $val) {
//                    $list[$key]['label'] = $val.'单元';
//                    $list[$key]['value'] = $val.'单元';
//                }
//                $v->children = $list;
//            } else {
//                $num = [];
//                for ($i = 1; $i<= $v->children->b_cell_num; $i++) {
//                    array_push($num, $i);
//                }
//                $list = [];
//                foreach ($num as $key => $val) {
//                    $list[$key]['label'] = $val.'单元';
//                    $list[$key]['value'] = $val.'单元';
//                }
//                $v->children = $list;
//            }
//            $v->label = $v->value;
//        }
        return $data;

    }
    /**
     * @param $type
     * @param $start
     * @param $end
     * @return array
     * 序列化前台传的值，转换成数组
     */
    public function sequenceArray($type, $start ,$end){
        $temp_arr = [];
        if($type  == 1){  //数字类型批量
            if(!is_numeric($start) || !is_numeric($end) || $start >= $end){     //判断是否为数字类型 开始数字不能大于等于结束数字 结束数字不能大于一次批量生成上线数字
                return ['code'=> -1];
            }
            if(intval($end)-intval($start) > env('BUILDING_LIMIT_ADMIN')) {
                return ['code'=> -2];
            }
            for($i = $start; $i <= $end; $i++){
                array_push($temp_arr, $i);
            }
        }else{    //字母类型批量
            if(!preg_match ("/^[A-Za-z]/", $start) || !preg_match ("/^[A-Za-z]/", $end)){ //判断是否输入为英文
                return ['code'=> -3];
            }
            $english_arr = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z');
            $start_num = array_search(strtoupper($start),$english_arr);
            $end_num = array_search(strtoupper($end),$english_arr);
            if ($start_num > $end_num) {
                return ['code' => -4];
            }
            for($i = $start_num; $i <= $end_num; $i++){
                array_push($temp_arr, $english_arr[$i]);
            }
        }
        return ['code' => 1, 'msg' => 'ok','data' => $temp_arr];
    }

    /**根据栋座ID 获取该栋座的名字
     * @param $bId 栋座ID
     * @return mixed
     */
    public function getWordbookInfoName($bId)
    {
        return DB::table($this->table)->where('b_id', $bId)->value('b_building');
    }

    /**根据栋座的名称获取栋座下面的所有单元信息
     * @param $bBuilding
     */
    public function getPositionInfo($bBuilding, $b_w_id)
    {
        $data = DB::table($this->table)
            ->where('b_w_id', $b_w_id)
            ->where('b_building', $bBuilding)
            ->select('b_id', 'b_cell_num', 'b_cell_unit')
            ->first();
        if(!$data){
            return false;
        }
        $list = [];
        if (self::$cell_unit[$data->b_cell_unit] == '英文') {
            $english_arr = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z');
            $num = [];
            for ($i = 0; $i< $data->b_cell_num; $i++) {
                array_push($num, $english_arr[$i]);
            }
            foreach ($num as $k => $v) {
                $list[$k]['b_id']  = $data->b_id;
                $list[$k]['value'] = $v.'单元';
            }
        } else {
            $num = [];
            for ($i = 1; $i<= $data->b_cell_num; $i++) {
                array_push($num, $i);
            }
            foreach ($num as $k => $v) {
                $list[$k]['b_id']  = $data->b_id;
                $list[$k]['value'] = $v.'单元';
            }
        }
        return $list;
    }





}